using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using FontAwesome.Sharp;
using log4net;
using Notifications.Wpf;
using Application = System.Windows.Application;

namespace YZAutoDWR.Handler;

public class ContextMenuHandler : IDisposable
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(ContextMenuHandler));
    private NotifyIcon? _notifyIcon;
    private readonly string _iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "report.ico");
    private const string AppName = "日报生成器";
    private readonly WeWorkHandler _weWorkHandler = App.Resolve<WeWorkHandler>();

    /** 初始化托盘菜单栏 */
    public void InitializeContextMenu()
    {
        if (!File.Exists(_iconPath))
        {
            Log.Error($"托盘图标路径异常: {_iconPath}");
            throw new FileNotFoundException($"无法找到托盘图标 '{_iconPath}'.");
        }

        // 初始化托盘图标
        _notifyIcon = new NotifyIcon
        {
            Icon = new System.Drawing.Icon(_iconPath),
            Visible = true,
            Text = AppName
        };
        var contextMenu = new ContextMenuStrip();

        contextMenu.Items.Add(new ToolStripMenuItem("更新个人工单信息",
            IconChar.List.ToBitmap(IconFont.Auto, 16, Color.Black), (_, _) => { UpdateWorkOrderList(); }));

        contextMenu.Items.Add(new ToolStripMenuItem("显示主窗口",
            IconChar.File.ToBitmap(IconFont.Auto, 16, Color.Black), (_, _) =>
            {
                if (Application.Current.MainWindow == null) return;
                Application.Current.MainWindow.Show();
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            }));

        contextMenu.Items.Add(new ToolStripSeparator()); // 分隔符

        contextMenu.Items.Add(new ToolStripMenuItem("退出",
            IconChar.ArrowRightFromBracket.ToBitmap(IconFont.Auto, 16, Color.Black), (_, _) =>
            {
                Dispose();
                Application.Current.Shutdown();
            }));

        _notifyIcon.ContextMenuStrip = contextMenu;

        _notifyIcon.DoubleClick += (_, _) => ShowMainWindow();
        _notifyIcon.Click += (_, e) =>
        {
            if (e is MouseEventArgs { Button: MouseButtons.Left })
            {
                ShowMainWindow();
            }
        };
    }

    private async void UpdateWorkOrderList()
    {
        try
        {
            var assignedToMe = await _weWorkHandler.UpdateWorkOrderList(2);
            var myProcessed = await _weWorkHandler.UpdateWorkOrderList(3);
            if (assignedToMe.Status && myProcessed.Status)
            {
                this.ShowToast("个人工单信息更新完成");
            }
        }
        catch (Exception e)
        {
            this.ShowToast("工单信息更新异常:", $"{e}", NotificationType.Information, 10);
        }
    }

    private static void ShowMainWindow()
    {
        if (Application.Current.MainWindow == null) return;

        Application.Current.MainWindow.Show();
        Application.Current.MainWindow.WindowState = WindowState.Normal;
        Application.Current.MainWindow.Activate();
    }

    /** 释放托盘图标资源 */
    public void Dispose()
    {
        if (_notifyIcon == null) return;

        _notifyIcon.Visible = false;
        _notifyIcon.Dispose();
        _notifyIcon = null;

        GC.SuppressFinalize(this);
    }
}